package com.polaris.live

import android.os.Looper
import android.util.Log
import com.google.firebase.crashlytics.FirebaseCrashlytics

/**
 * 全局异常捕获类
 */
class CustomUncaughtExceptionHandler(
    private val defaultHandler: Thread.UncaughtExceptionHandler? = Thread.getDefaultUncaughtExceptionHandler()
) : Thread.UncaughtExceptionHandler {

    override fun uncaughtException(t: Thread, e: Throwable) {
        Log.e(TAG, "Thread: $t", e)

        //主线程崩溃直接退出
        val mainThread = Looper.getMainLooper().thread
        if (t == mainThread && e is RuntimeException) {
            defaultHandler?.uncaughtException(t, e)
        } else {
            FirebaseCrashlytics.getInstance().recordException(e)
        }
    }

    companion object {

        const val TAG = "ErrorHandler"
    }
}